package com.hdax.controller;

import com.hdax.entity.*;
import com.hdax.service.EmployeeInfoService;
import com.hdax.service.MenuService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页控制器
 *
 * @author Aizen
 * @date 2022/05/06
 */
@Controller
public class IndexController {

    /**
     * 菜单服务
     */
    @Autowired
    private MenuService menuService;

    @Autowired
    private EmployeeInfoService employeeInfoService;

    /**
     * 登录成功后跳转到这里
     *
     * @return {@link String}
     */
    @GetMapping("/index")
    public String index(HttpSession session){
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        session.setAttribute("loginUser", employee);
        return "index";
    }

    @GetMapping("/home")
    public String home(){
        return "Home";
    }

    /**
     * 获取一级菜单
     *
     * @param session 会话
     * @return {@link Map}<{@link String}, {@link Object}>
     */
    @GetMapping(path = "/menu")
    @ResponseBody
    public Map<String,Object> menu(HttpSession session){
        Employee employee = (Employee)session.getAttribute("loginUser");
        if(employee.getEmpId()==1){
            employee.setRoleId(1);
            Role role = new Role();
            role.setId(1);
            employee.setRole(role);
        }
        Role role = employee.getRole();
        List<Menu> menus = menuService.menus(role.getId());
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("homeInfo", HomeInfo.builder().title("首页").href("/home").build());
        map.put("logoInfo", LogoInfo.builder().title("人事管理系统").href("").image("layuimini/images/logo.png").build());
        Menu menu = new Menu();

        menu.setTitle("人事管理系统");
        menu.setChild(menus);
        map.put("menuInfo", Arrays.asList(menu));
        return map;
    }





}
